import Vue from 'vue'
import Router from 'vue-router'
//引入组件
import Login from '../views/Login.vue'
import Home from '../views/Home.vue'
import Mycenter from '../views/home/Mycenter.vue'
import Product from '../views/product/Product.vue'
import Saleproduct from '../views/product/Saleproduct.vue'
import Cate from '../views/product/Cate.vue'
import Brand from '../views/product/Brand.vue'
import Baseattr from '../views/product/Baseattr.vue'
import Salesattr from '../views/product/Salesattr.vue'
import Addproduct from '../views/product/Addproduct'
import Order from '../views/order/Order.vue'
import News from '../views/news/News'
import AddNews from '../views/news/AddNews'
import Comment from '../views/comment/Comment.vue'
import Statistical from '../views/statistical/Statistical.vue'
import Userlist from '../views/user/Userlist.vue'


Vue.use(Router)

const router = new Router({
  routes:[
    // 配置路由规则
    // 重定向
    {
      path:'/',redirect:'./login'
    },
   {
     path:'/login' ,component:Login
   },
   {
    path:'/home' ,component:Home,
    redirect:'/statistical',
    children:[
      {
        path:'/mycenter',
          component:Mycenter
      },
    {
      path:'/userlist',
        component:Userlist
    },
    {
      path:'/product',
        component:Product
    },
    {
      path:'/saleproduct',
        component:Saleproduct
    },
    {
      path:'/categories',
        component:Cate
    },
    {
      path:'/brand',
        component:Brand
    },
    {
      path:'/baseattr',
        component:Baseattr
    },
    {
      path:'/salesattr',
        component:Salesattr
    },
    {
      path:'/addproduct',
        component:Addproduct
    },
    {
      path:'/order',
        component:Order
    },
    {
      path:'/news',
        component:News
    },
    {
      path:'/addnews',
        component:AddNews
    },
    {
      path:'/comment',
        component:Comment
    },
    {
      path:'/statistical',
        component:Statistical
    }
    ]
  }
  ]
});

//挂在路由导航守卫
router.beforeEach((to,from,next) =>{
  //首先判断是不是重登录页面来
  if(to.path === '/login')return next();
  //获取token，判断是否是有效token
  const isToken = window.sessionStorage.getItem("token")
  if(!isToken)  return next('/login')
  next()
})

export default router